<?php
/*********************************************************************
 * ADVERTISEMENT CLASS FILE
 * 
 * Author: hthieu1110
 * Date: 10/04/2011
 **********************************************************************
 * function retrieveFieldList($tableName):
 *   + retrieve all the fields name of advertisement for generate automatically the function get/set
 * 
 * function retrieveRequiredFieldList($args):
 *   + retrieve all the required fields of advertisement for check before add an advertisement into db
 *
 * function execQuery($type, $query):
 *   + execute an sql query by type : insert, update, select
 *   
 **********************************************************************/
  require_once dirname(realpath(__FILE__)).'./../config/config.inc.php';

  class Db{
    static public $dbLink = null;
    static private $queryCounter = 0;
    static public $debugContent = '';
    
#---------------------------------------------------------------------------
    private function debugSQL($query){
      Db::$queryCounter++;
      if ((ENV == 'dev' && isset($_REQUEST['dbg'])) == false) return true;
      ob_start();
      $backTrace = debug_backtrace();
      echo '<div style="font-size:11px;font-family: Tahoma;background-color: #ffffff;">';
      echo '<div style="background-color: #666666; color: #ffffff; font-weight:bold; padding: 1px;">Query number '.Db::$queryCounter.'</div>';
      for($i=count($backTrace)-1; $i>=0; $i--) {
        $num = count($backTrace) - $i;
        echo '<div>#'.$num.' => <i>'.$backTrace[$i]['file'].' ('.$backTrace[$i]['line'].'): '.$backTrace[$i]['function'].'()</i></div>';
      }
      echo '<div>Query : <b>'.$query.'</b></div>';
      echo '</div>';
      echo '<hr style="margin:0;padding:0;" />';
      Db::$debugContent .= ob_get_contents();
      ob_end_clean();
    }
    
#---------------------------------------------------------------------------
    public function  __construct(){
      if(!is_null(self::$dbLink)){
        return true;
      }

      if (!self::$dbLink = @mysql_connect(DB_HOST, DB_USER, DB_PASS)){
        throw new Exception('<br/>File   : <b>'.__FILE__.'</b>
        										 <br/>Error  : <i>Cannot connect to DB</i>
        										 <br/>Class  : <i>'.__CLASS__.'</i>
        										 <br/>Method : <i>'.__METHOD__.'</i>
        										 <br/>Command: <i>mysql_connect('.DB_HOST.', '.DB_USER.', '.DB_PASS.')</i>
        										 ');
      }
      
      if (!@mysql_select_db(DB_NAME)){
        throw new Exception('<br/>File   : <b>'.__FILE__.'</b>
        										 <br/>Error  : <i>Cannot select the DB</i>
        										 <br/>Class  : <i>'.__CLASS__.'</i>
        										 <br/>Method : <i>'.__METHOD__.'</i>
        										 <br/>Command: <i>mysql_select_db('.DB_NAME.')</i>
        									 ');
      }

      mysql_query("SET NAMES UTF8"); //important for UTF 8 characters
      return true;
    }
    
#---------------------------------------------------------------------------
    public function __destruct(){
      //if (is_null(self::$dbLink)){
      //  return true;
      //}

      //if (!mysql_close(self::$dbLink)){
      //  return false;
      //}

      return true;
    }
   
#---------------------------------------------------------------------------
    static public function retrieveFieldList($tableName){
    	global $_advertisement_fields;
    	
			switch ($tableName) {
				case 'advertisement':
					return $_advertisement_fields;
					break;
			}
    }

#---------------------------------------------------------------------------
    static public function retrieveRequiredFieldList($tableName){
			global $_advertisement_required_fields;
			
    	switch ($tableName) {
				case 'advertisement':
					return $_advertisement_required_fields;
					break;
			}
    }

#---------------------------------------------------------------------------
    public function update($query) {
      if (ENV == 'dev') $this->debugSQL($query);
      
      $result = @mysql_query($query);
      
      if (!$result) throw new Exception('<br/>File   : <b>'.__FILE__.'</b><br/>Error  : <i>'.mysql_error().'</i><br/>Class  : <i>'.__CLASS__.'</i><br/>Method : <i>'.__METHOD__.'</i><br/>Command: <i>mysql_query('.$query.')</i>');
      
      return true;
    }
    
#---------------------------------------------------------------------------
    public function insert($query) {
      if (ENV == 'dev') $this->debugSQL($query);
      
      $result = @mysql_query($query);
      
      if (!$result) throw new Exception('<br/>File   : <b>'.__FILE__.'</b><br/>Error  : <i>'.mysql_error().'</i><br/>Class  : <i>'.__CLASS__.'</i><br/>Method : <i>'.__METHOD__.'</i><br/>Command: <i>mysql_query('.$query.')</i>');
      
      return mysql_insert_id();
    }
#---------------------------------------------------------------------------
/**
 * Get the result of SQL SELECT query.
 * @param string $query SQL query
 * @param string $className Name of class to which the record will be assign, if it's not set then return the value direct by mysql_fetch_assoc
 * @throws Exception
 */
    public function selectTable($query, $className = '') {
      if (ENV == 'dev') $this->debugSQL($query);
      
      $results = @mysql_query($query);
      
      if (!$results) throw new Exception('<br/>File   : <b>'.__FILE__.'</b><br/>Error  : <i>'.mysql_error().'</i><br/>Class  : <i>'.__CLASS__.'</i><br/>Method : <i>'.__METHOD__.'</i><br/>Command: <i>mysql_query('.$query.')</i>');
      
      if ($className != '') {
      	while($entry = mysql_fetch_object($results, $className, array('', true))) {
  			  $entries[] = $entry;
  			}
      } else {
        while($entry = mysql_fetch_assoc($results)) {
  			  $entries[] = $entry;
  			}
      }

			return $entries;
    }

#---------------------------------------------------------------------------
      public function select($query, $className = '') {
      if (ENV == 'dev') $this->debugSQL($query);

      $result = @mysql_query($query);

      if (!$result) throw new Exception('<br/>File   : <b>'.__FILE__.'</b><br/>Error  : <i>'.mysql_error().'</i><br/>Class  : <i>'.__CLASS__.'</i><br/>Method : <i>'.__METHOD__.'</i><br/>Command: <i>mysql_query('.$query.')</i>');

      if ($className != '') {
    	  $entry = mysql_fetch_object($result, $className, array('', true));
    	} else {
    	  $entry = mysql_fetch_assoc($result);
    	}

			return $entry;
    }
  }
?>
